#include <gtk/gtk.h>

// 按钮按下的处理函数, gpointer 相当于 void *
void deal_pressed(GtkButton *button, gpointer user_data)
{
	// 获得按钮的文本信息
	const gchar *text = gtk_button_get_label( button );
	// g_print() 相当于C语言的 printf(), gchar相当于char
	g_print("button_text = %s; user_data = %s\n", text, (gchar *)user_data);
}

int main(int argc,char *argv[])
{
	gtk_init(&argc,&argv);
	
	GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	
	// 设置窗口边框的宽度(窗口里的控件与窗口边框间隔为50)
	gtk_container_set_border_width(GTK_CONTAINER(window),50);
	
	/* 信号连接函数当窗口关闭时，窗口会触发destroy信号,信号回调;
	 * 当窗口关闭时，窗口会触发destroy信号,
	 * 自动调用gtk_main_quit()结束程序运行
	 * 窗口关闭时触发的常用信号:destroy、delete-event;
	 */
	g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);
	
	// 创建按钮, 文本信息为"Hello Gtk+";
	GtkWidget *button = gtk_button_new_with_label("Hello GTK+");
	
	// 获得按钮的内容;
	const char *str = gtk_button_get_label(GTK_BUTTON(button));
	printf("-> str = %s\n",str);
	
	// 把按钮添加到窗口容器;
	gtk_container_add(GTK_CONTAINER(window),button);
	
	/* 按钮按下(pressed)后会自动调用deal_pressed()
	 * "is pressed"是传给deal_pressed()的数据
	 * 操作按钮触发的常用信号:clicked、pressed、released;
	 */
	g_signal_connect(button, "pressed", G_CALLBACK(deal_pressed), "is pressed");
	
	// 显示控件有两种方法:逐个显示,全部显示;
	// gtk_widget_show(button);
	// gtk_widget_show(window);
	
	gtk_widget_show_all(window);
	
	gtk_main();
	
	return 0;
}